/**
 * 
 */
package aiproject3.agents;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import aiproject3.models.KnowledgeBase;
import aiproject3.predictors.Predictor;

/**
 * @author Chris
 *
 */
public class TrainingAgent extends Agent<Character, Integer> {

	public TrainingAgent(KnowledgeBase<Character, Integer> data,
			Predictor<Character> predictor) {
		super(data, predictor);
		// TODO Auto-generated constructor stub
	}

	public void train(String trainingFile) {
		try {
			File file = new File(trainingFile);
			if (!file.exists()) {
				System.out.println("Error: Unable to train model... Training file not found.");
			} else {
				
				FileInputStream fstream = new FileInputStream(file);
			    DataInputStream in = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(in));
			    
			    String strLine;
			    
			    while ((strLine = br.readLine()) != null) {
		    		// Break up input-string into 'array' of elements
		    		String[] row = strLine.split("\\s*,\\s*");
		    		for (String unit : row) {
		    			if (unit.length() != 1) {
		    				System.out.println("Error: Malformed input in training file... Expecting a single character.");
		    			} else {
		    				addToModel(unit.charAt(0));
		    			}
		    		}
			    }
			    in.close();
			}
		} catch (IOException ex) {
			System.out.println("Error: Unable to access training file... \n" + ex.getMessage());
		}
	}

}
